<?php
/**
 * common repository file Created by PhpStorm.
 * User: yang
 * Date: 2020/06/11
 */

namespace Common\Repository;

use Common\Model\User;

class UserRepository extends BaseRepository
{
    public function baseModel()
    {
        return new User();
    }

    /**
     * 统计数量
     * @return mixed
     */
    public function extendCount()
    {
        $month = date("Y-m-d", strtotime("-1 month"));//一月前时间
        $data['month'] = User::where('updated_at', '>', $month)->count();
        $data['total'] = sprintf("%.2f", $data['month'] / User::count()) * 100;

        return $data;
    }

    /**
     *
     */
    public function extendActiveUsers()
    {
        //$week = date("Y-m-d", strtotime("-1 week"));//一周前时间
        $month = date("Y-m-d", strtotime("-1 month"));//一月前时间
        return User::select(['id', 'name', 'login_times', 'head_img', 'updated_at'])
            ->where('created_at', '>', $month)
            ->withCount(['get_comment', 'get_zan'])
            ->orderBy('login_times', 'desc')
            ->orderBy('get_comment_count', 'desc')
            ->orderBy('get_zan_count', 'desc')
            ->limit(10)
            ->get()
            ->toArray();
    }
}